package com.ledtechdesign.beans;

import java.io.InputStream;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;

import com.ledtechdesign.afcs.IAuthToken;
import com.ledtechdesign.afcs.ISession;
import com.ledtechdesign.auth.AuthenticationFactory;
import com.ledtechdesign.service.Utils;

public class Session implements ISession {


	Log logger = LogFactory.getLog(this.getClass());
	
	// old
	public String roomInstanceURI;
	private Account account;
	private String room;
	private String sessionSecret;
	// new 
	
	public Session(Account account,String sessionSecret,String roomInstanceURIParam,String roomParam) {
		if (roomInstanceURIParam == null || roomParam==null) {
			throw new RuntimeException("Can not Create session when there is"
					+ "no Rooms, Create Rooms First");
		}
		this.sessionSecret = sessionSecret;
		this.roomInstanceURI = roomInstanceURIParam;
		this.account = account;
		this.room = roomParam;
	}
	

	public String getSessionSecret() {
		return sessionSecret;
	}


	public String getRoom() {
		return room;
	}


	public Account getAccount() {
		return account;
	}

	/**
	 * get an external authentication token
	 */
	
	private IAuthToken mytoken;
	public IAuthToken getAuthenticationToken(String accountSecret, String name,
			String id, int role) throws Exception {
		
		if(accountSecret==null || accountSecret.trim().equals("")){
			throw new Exception("Account Secret is Required to Generate Session Auth Token");
		}
		
		User user = new User(name,id,role);
		
		if(mytoken==null){
			mytoken = AuthenticationFactory.getSessionAuthToken(user, this);
		}
		
		logger.debug("ext-token is:  "+mytoken);
		return mytoken;
	}


}
